<template>
  <div class="popup-container">
    <!-- 弹出层的头部区域 -->
    <van-nav-bar title="频道管理">
      <template #right>
        <van-icon name="cross" size="14" color="white" @click="close" />
      </template>
    </van-nav-bar>

    <!-- 弹出层的主体区域 -->
    <div class="pop-body">
      <!-- 我的频道 -->
      <div class="my-channel-box">
        <div class="channel-title">
          <div>
            <span class="title-bold">已添加频道：</span>
            <span class="title-gray">点击{{isdel ? '删除':'进入'}}频道</span>
          </div>
          <span class="btn-edit" @click="edit">{{ isdel ? '完成' : '编辑' }}</span>
        </div>
        <!-- 我的频道列表 -->
        <van-row type="flex">
          <van-col span="6" v-for="item in added" :key="item.id">
            <!-- 用户的频道 Item 项 -->
            <div class="channel-item van-hairline--surround" @click="del(item)">
              {{ item.name }}
              <van-badge color="transparent" class="cross-badge" v-show="isdel && item.id !== 0">
                <template #content>
                  <van-icon name="cross" class="badge-icon" color="#cfcfcf" size="12" />
                </template>
              </van-badge>
            </div>
            <!-- 删除的图标 -->
          </van-col>
        </van-row>
      </div>

      <!-- 分隔线 -->
      <div class="van-hairline--top sp-line"></div>

      <!-- 更多频道 -->
      <div class="more-channel-box">
        <div class="channel-title">
          <div>
            <span class="title-bold">可添加频道：</span>
            <span class="title-gray">点击添加频道</span>
          </div>
        </div>
        <!-- 更多频道列表 -->
        <van-row type="flex">
          <van-col span="6" v-for="item in unChannel" :key="item.id">
            <div class="channel-item van-hairline--surround" @click="mortFn(item)">{{ item.name }}</div>
          </van-col>
        </van-row>
      </div>
    </div>
  </div>
</template>

<script>
// 目标1：编辑频道->显示xhe不同的文字
// 1. 定义isdek变量 来控制x是否显示
// 2. 编辑span标签点击时 isdel取反
// 3 isdel变脸控制文字的不同显示， 以及推荐频道上x 额外判断条件
// import { deleteUserChannelAPI } from '@/api/index.js'
export default {
  props: {
    added: Array, // 用户已选
    unChannel: Array // 用户未选

  },
  data () {
    return {
      isdel: false,
      switchs: '编辑'
    }
  },
  methods: {
    // 关闭弹出层
    close () {
      this.$emit('close', false)
      this.isdel = false
    },
    edit () {
      this.isdel = !this.isdel
    },
    // 目标2：编辑频道后 -> 点击更多频道进行新增
    // 1 更多频道->绑定点击事件 转递频道对象下去
    // 2 子->父 把频道对象转递给Home/index，vue -> 父push到用户已选的频道数组内即可
    mortFn (obj) {
      if (this.isdel) {
        // this.added.push(obj)
        this.$emit('ChannelEV', obj)
      }
    },
    // 删除
    async del (item) {
      if (this.isdel) {
        if (item.id !== 0) {
          this.$emit('deleteObj', item)
        }
        // 接口问题
        // const { data: res } = await deleteUserChannelAPI({ channelsId: id })
      } else {
        this.$emit('close', false)
        this.$emit('input', item.id)
        // 触发v-model input 绑定事件
      }
    }
  }
}
</script>

<style lang="less" scoped>
.van-nav-bar {
  background-color: #0077ff;
}
/deep/ .van-nav-bar__title {
  color: #fff;
}
.van-nav-bar .van-icon {
  color: #fff;
}
.van-popup,
.popup-container {
  background-color: transparent;
  height: 100%;
  width: 100%;
}

.popup-container {
  display: flex;
  flex-direction: column;
}

.pop-body {
  flex: 1;
  overflow: scroll;
  padding: 8px;
  background-color: white;
}

.my-channel-box,
.more-channel-box {
  .channel-title {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    line-height: 28px;
    padding: 0 6px;
    .title-bold {
      font-weight: bold;
    }
    .title-gray {
      color: gray;
      font-size: 12px;
    }
  }
}

.btn-edit {
  border: 1px solid #a3a2a2;
  padding: 0 10px;
  line-height: 20px;
  height: 20px;
  border-radius: 6px;
  font-size: 12px;
}

.channel-item {
  font-size: 12px;
  text-align: center;
  line-height: 36px;
  background-color: #fafafa;
  margin: 6px;
}

.cross-badge {
  position: absolute;
  right: -3px;
  top: 0;
  border: none;
}

.sp-line {
  margin: 10px 0 20px 0;
}

</style>
